package ru.amse.baltijsky.javascheme.nodeshape;

import ru.amse.baltijsky.javascheme.util.Dir;
import ru.amse.baltijsky.javascheme.util.Size;
import ru.amse.baltijsky.javascheme.util.VisConst;

import java.awt.*;

/**
 * Inital automata-style shape with a circle and an arrow pointing from the left.
 */
public class InitialShape extends AbstractNodeShape {
    /**
     * Default constructor for an initial shape with no arguments.
     * Intended to use instead of the usual version with String.
     */
    public InitialShape() {
        super("");
    }

    /**
     * Constructs an initial shape, taking the ignored string parameter (there is no code in the initial state).
     * Used only for compatibility with major type shapes.
     */
    public InitialShape(String code) {
        this();
    }

    public void draw(int x, int y, Graphics g) {
        int radius = ShapeTools.getCircleRadius(g);
        Color oldColor = g.getColor();
        g.setColor(VisConst.SCHEME_COL);

        // the circle
        g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
        // the arrow
        g.drawLine(x - radius * 2, y, x - radius, y);
        int arrowLen = radius / 3;
        double alpha = Math.PI / 6;
        int arrowX = (int) (arrowLen * Math.cos(alpha));
        int arrowY = (int) (arrowLen * Math.sin(alpha));
        g.drawLine(x - radius - arrowX, y - arrowY, x - radius, y);
        g.drawLine(x - radius - arrowX, y + arrowY, x - radius, y);

        g.setColor(oldColor);
    }

    public Size getBoundRectSize(Graphics g) {
        int[] dims = getShapeDims(g);
        return new Size(dims[Dir.l()] + dims[Dir.r()], dims[Dir.u()] + dims[Dir.d()]);
    }

    @Override
    public int[] getShapeDims(Graphics g) {
        final int radius = ShapeTools.getCircleRadius(g);
        int[] ret = new int[Dir.length()];
        ret[Dir.l()] = 2 * radius;
        ret[Dir.u()] = radius;
        ret[Dir.r()] = radius;
        ret[Dir.d()] = radius;
        return ret;
    }
}
